package niord.apps.database;

import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DataBaseTables {

	// Product table creation SQL statement
	private static final String create_products = "create table products ("
			+ "id integer primary key autoincrement, " 
			+ "name text not null, "
			+ "price real not null, " 
			+ "type_id integer not null, "
			+ "uom_id integer not null, " 
			+ "place_id integer not null);";

	// Product table creation SQL statement
	private static final String create_persons = "create table persons ("
			+ "id integer primary key autoincrement, " 
			+ "name text not null);";

	// Order table creation SQL statement
	private static final String create_orders = "create table orders ("
			+ "id integer primary key autoincrement, "
			+ "person_id integer not null, " 
			+ "product_id integer not null, "
			+ "quantity integer not null);";

	// Place table creation SQL statement
	private static final String create_places = "create table places ("
			+ "id integer primary key autoincrement, " 
			+ "name string not null, "
			+ "address string not null, " 
			+ "phone string not null);";

	// Delete statements
	private static final String delete_products = "DROP TABLE IF EXISTS products";
	private static final String delete_persons = "DROP TABLE IF EXISTS persons";
	private static final String delete_orders = "DROP TABLE IF EXISTS orders";
	private static final String delete_places = "DROP TABLE IF EXISTS places";

	// Create all tables in database
	public static void onCreate(SQLiteDatabase database) {
		database.execSQL(create_products);
		database.execSQL(create_persons);
		database.execSQL(create_orders);
		database.execSQL(create_places);
	}

	// Upgrade DB with new tables
	public static void onUpgrade(	SQLiteDatabase database,
											int oldVersion,
											int newVersion) {
		Log.w(DataBaseTables.class.getName(), "Upgrading database from version "
				+ oldVersion + " to " + newVersion
				+ ", which will destroy all old data");

		database.execSQL(delete_products);
		database.execSQL(delete_persons);
		database.execSQL(delete_orders);
		database.execSQL(delete_places);
		onCreate(database);
	}

}
